package com.client.koshercook.client;

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.dom.client.KeyCodes;
import com.google.gwt.event.dom.client.KeyUpEvent;
import com.google.gwt.event.dom.client.KeyUpHandler;
import com.google.gwt.event.dom.client.MouseOutEvent;
import com.google.gwt.event.dom.client.MouseOutHandler;
import com.google.gwt.event.dom.client.MouseOverEvent;
import com.google.gwt.event.dom.client.MouseOverHandler;
import com.google.gwt.i18n.client.HasDirection.Direction;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.DialogBox;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.HasAlignment;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Image;
import com.google.gwt.user.client.ui.ListBox;
import com.google.gwt.user.client.ui.TextArea;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.VerticalPanel;

public class AddRecipeBox extends Composite {
	
	private Image open = new Image();
	private Button ok = new Button("אישור");
	private Button cancel = new Button("ביטול");
	private DialogBox dBox = new DialogBox();
	private VerticalPanel Vpanel = new VerticalPanel();
	private HorizontalPanel HpanelUp = new HorizontalPanel();
	private HorizontalPanel HpanelDown = new HorizontalPanel();
	private TextBox textName = new TextBox();
	private TextArea textInst = new TextArea();
	private TextArea textIngs = new TextArea();
	private ListBox recType = new ListBox();
	
	public AddRecipeBox(){
		textName.setDirection(Direction.RTL);
		textInst.setDirection(Direction.RTL);
		textIngs.setDirection(Direction.RTL);
		
		textInst.setCharacterWidth(80);
		textInst.setVisibleLines(5);
		
		textIngs.setCharacterWidth(80);
		textIngs.setVisibleLines(5);
		
		recType.addItem("אחר");
		recType.addItem("מנות ראשונות");
		recType.addItem("בשר ודגים");
		recType.addItem("מרקים");
		recType.addItem("תוספות");
		recType.addItem("סלטים");
		recType.addItem("לחמים");
		recType.addItem("קינוח");
		recType.addItem("עוגות");
		recType.setStyleName("gwt-TextBox");
		
		open.setUrl("addRecipe_off.gif");
		open.setSize("132px", "24px");
		open.addMouseOverHandler(new MouseOverHandler(){
			public void onMouseOver(MouseOverEvent event) {
				open.setUrl("addRecipe_on.gif");
			}
		});
		open.addMouseOutHandler(new MouseOutHandler(){
			public void onMouseOut(MouseOutEvent event) {
				open.setUrl("addRecipe_off.gif");
			}
		});
		open.setTitle("הוסף מתכון משלך");
		open.addClickHandler(new ClickHandler(){
			public void onClick(ClickEvent event) {
				if(!Client.model.isConnected()){
					Client.model.showLogMessage("להוסיף מתכון לאתר");
				}else{
					dBox.center();
					
					textName.setFocus(true);
				}
				open.setUrl("addRecipe_off.gif");
			}
		});
		
		ok.addClickHandler(new ClickHandler(){
			public void onClick(ClickEvent event) {
				String recipeName = textName.getText();
				String instructions = textInst.getText();
				String ingredients = textIngs.getText();
				String type = recType.getItemText(recType.getSelectedIndex());
				if(recipeName.equals("") || instructions.equals("") || ingredients.equals("")){
					Window.alert("עליך למלא את כל השדות");
					return;
				}
				Client.model.sendNewRecipe(recipeName, instructions, ingredients, type);
				clear();
			}
		});
		
		cancel.addClickHandler(new ClickHandler(){
			public void onClick(ClickEvent event) {
				clear();
			}
		});
		
		textName.addKeyUpHandler(new KeyUpHandler(){
			public void onKeyUp(KeyUpEvent event) {
				if (event.getNativeKeyCode() == KeyCodes.KEY_ESCAPE) {
					dBox.hide();
					textName.setFocus(false);
				}
			}
		});
		
		Vpanel.setHorizontalAlignment(HasAlignment.ALIGN_RIGHT);
		Vpanel.add(new HTML(":שם המתכון"));
		Vpanel.add(HpanelUp);
		HpanelUp.add(recType);
		HpanelUp.add(new HTML(":סוג"));
		HpanelUp.add(textName);
		Vpanel.add(new HTML(":מרכיבים"));
		Vpanel.add(textIngs);
		Vpanel.add(new HTML(":הוראות הכנה"));
		Vpanel.add(textInst);
		Vpanel.add(HpanelDown);
		HpanelDown.add(cancel);
		HpanelDown.add(ok);
		HpanelDown.setSpacing(3);
		dBox.add(Vpanel);
		dBox.setText("הוסף מתכון משלך");
		dBox.setGlassEnabled(true);
		dBox.setAnimationEnabled(true);
		clear();
		
		initWidget(open);
	}
	
	/** clear text fields and hide the dialog box */
	private void clear(){
		dBox.hide();
		textName.setText("");
		textIngs.setText("");
		textInst.setText("");
		recType.setSelectedIndex(0);
	}
}
